
const db = wx.cloud.database()
const teamlistCollection = db.collection('teamlist')
const teamtypeList=[
  {
    typename:'一起比赛'
},
{
  typename:'一起租房'
},
{
typename:'一起出去玩'
},
{
typename:'其他'
}
]
Page({
  data: {
    teamtypeList,
    teamtypeindex:0,
    contesttype: '',
    currentdate: '2021-09-01',
    enddate: '2022-09-01',
    number: 0,
    openid:'',
    nickname:'',
    phoneNum:'',
    imgPath:'',
    teamimg:''
  },
  onLoad: function () {
    this.setData({
      openid: wx.getStorageSync('OPENID'),
      nickname: wx.getStorageSync('NICKNAME'),
    })
  },
  uploadTeamImg() {
    let that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        wx.showLoading({
          title: '上传中',
        });
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let filePath = res.tempFilePaths[0];
        that.setData({
          imgPath: filePath
        })
        const name = Math.random() * 1000000;
        const cloudPath = name + filePath.match(/\.[^.]+?$/)[0]
        wx.cloud.uploadFile({
          cloudPath,//云存储图片名字
          filePath,//临时路径
          success: res => {
            console.log('[上传图片] 成功：', res)
            that.setData({
              teamimg: res.fileID,//云存储图片路径,可以把这个路径存到集合，要用的时候再取出来
            });
          },
           fail: e => {
            console.error('[上传图片] 失败：', e)
          },
          complete: () => {
            wx.hideLoading()
          }
        });
      }
    })
  },
  bindPickerChange: function (e) {
    let index = e.detail.value
    this.setData({
      teamtype: teamtypeList[index].typename,
      teamtypeindex: index
    })
    console.log(this.data.teamtype)
  },
  bindStartdateChange: function (e) {
    this.setData({
      enddate: e.detail.value,
    })
  },
  getPhoneNumber (e) {
    let cloudID = e.detail.cloudID //开放数据ID
    if (!cloudID) {
        app.showToast('用户未授权')
        return
    }
    // 调用云函数获取手机号
    wx.cloud.callFunction({
        name: 'getPhoneNum',
        data: {
          weRunData: wx.cloud.CloudID(e.detail.cloudID),
        }
      })
      .then( res => {
          console.log('手机号', res.result)
          this.setData({
            phoneNum:  res.result
          })
      })
      .catch( err => {
        console.log('手机号err', err)
      })
  },
  formSubmit: function (e) {
    let data = e.detail.value
    let datavalid = this.checkdata(data)
    if (!datavalid) {
      return
    }
    Object.assign(
      data,
      { openid: this.data.openid },
      { nickname: this.data.nickname },
      { date: Date.now() },
      { teamimg: this.data.teamimg },
      { member: [] }
    )
    teamlistCollection.add({
      data: data,
      success: (res) => {
        console.log(res)
        wx.showToast({
          title: '发布成功',
          duration: 3000,
          success: (res)=>{
            wx.navigateBack({
              delta: 1,
            })
          }
        },
        
        )
      },
    })
  },
  formReset: function () {
    console.log('form发生了reset事件')
  },
  checkdata(data) {
    console.log(data)
    if (
      !data.teamname ||
      !data.teamtype ||
      !data.phone ||
      !data.vacancy ||
      !data.requirement
    ) {
      wx.showToast({
        title: '发布失败，请将信息填写完整！',
        icon: 'none',
        image: '',
        duration: 2000,
        mask: false,
        success: (result) => {},
        fail: () => {},
        complete: () => {},
      })
      return false
    }else{
      return true
    }
  },
})
